package com.acquisition.param.dao.impl;

//import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.acquisition.param.dao.ParamDao;
import com.acquisition.param.domain.Param;
import com.acquisition.task.constants.SQLConstants;
import com.acquisition.task.database.dao.BaseDao;
import com.acquisition.task.database.dao.impl.BaseDaoImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//import com.dcs.cn.common.constants.SQLConstants;
//import com.dcs.cn.common.database.dao.BaseDao;
//import com.dcs.cn.common.database.dao.impl.BaseDaoImpl;
//import com.dcs.cn.param.dao.ParamDao;
//import com.dcs.cn.param.domain.Param;



import com.acquisition.param.dao.ParamDao;
/**
 * 参数CURD实现
 *
 * @author wuqiwei
 * @date 2021/01/04
 */
public class ParamDaoImpl implements ParamDao {

	@Override
	public int save(Param param) {
		BaseDao<Param> dao = (BaseDao<Param>) new BaseDaoImpl<>();
		int count = 0;
		try {
			count = dao.save(param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public int remove(int id) {
		BaseDao<Param> dao = (BaseDao<Param>) new BaseDaoImpl<>();
		int count = 0;
		try {
			count = dao.remove(id, Param.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public int update(Param param) {
		BaseDao<Param> dao = (BaseDao<Param>) new BaseDaoImpl<>();
		int count = 0;
		try {
			count = dao.update(param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public int update(String key, String value, int type) {
		int count = 0;
		try {
			BaseDao<Param> dao = (BaseDao<Param>) new BaseDaoImpl<>();
			String tableName = dao.getTableName(Param.class);
//			ContentValues contentValues = new ContentValues();
//			contentValues.put("value", value);
//			contentValues.put("type", type);

			//TODO 改成mybits-plus的 mapper
//			SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(SQLConstants.DATABASE_PATH, null, SQLiteDatabase.OPEN_READWRITE);
//			count = sqLiteDatabase.update(tableName, contentValues, "key = ?", new String[]{key});
//			sqLiteDatabase.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public Param get(int id) {
		BaseDao<Param> dao = (BaseDao<Param>) new BaseDaoImpl<>();
		Param param = new Param();
		try {
			param = dao.get(id, Param.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return param;
	}

	@Override
	public String getValue(String key) {
		String value = null;
		try {
			BaseDao<Param> dao = (BaseDao<Param>) new BaseDaoImpl<>();
			String tableName = dao.getTableName(Param.class);
			SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(SQLConstants.DATABASE_PATH, null, SQLiteDatabase.OPEN_READWRITE);
			Cursor cursor = sqLiteDatabase.query(tableName, new String[]{"value"}, "key = ?", new String[]{key}, null, null, null);
			cursor.moveToFirst();
			value = cursor.getString(0);
			cursor.close();
			sqLiteDatabase.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	@Override
	public List<Param> queryByExample(Map<String, Object> param) {
		BaseDao<Param> dao = (BaseDao<Param>) new BaseDaoImpl<>();
		List<Param> list = new ArrayList<>();
		try {
			list = dao.queryByExample(param, Param.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int count(Map<String, Object> param) {
		BaseDao<Param> dao = (BaseDao<Param>) new BaseDaoImpl<>();
		int count = 0;
		try {
			count = dao.count(param, Param.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

}
